<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="renderer" content="webkit"/>
  <meta name="force-rendering" content="webkit"/>
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>
  <title>素材管理</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <link rel="stylesheet" href="/bower_components/bootstrap/dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="/bower_components/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="/bower_components/Ionicons/css/ionicons.min.css">
  <link rel="stylesheet" href="/bower_components/datatables.net-bs/css/dataTables.bootstrap.min.css">
  <link rel="stylesheet" href="/dist/css/AdminLTE.min.css">
  <link rel="stylesheet" href="/dist/css/skins/_all-skins.min.css">

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->

  <!-- Google Font -->
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

  <!--头部-->
  <header id="headerDiv" class="main-header">
    <!--#include  file="/pages/admin/header.html"-->
  </header>

  <!--左侧边栏 -->
  <aside id="sidebarDiv" class="main-sidebar">
    <!--#include  file="/pages/admin/sidebar.html"-->
  </aside>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        <small>素材管理</small>
      </h1>
      <ol class="breadcrumb">
        <li><i class="fa fa-buysellads"></i> 渠道管理管理</li>
        <li><a href="javascript:goBack();">渠道列表</a></li>
        <li>素材管理</li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">
      <div class="row">
        <div class="col-xs-12">
          <div class="box box-primary">
            <div class="box-header with-border">
              <h3 class="box-title">素材管理</h3>
            </div>
            <!-- /.box-header -->
            <!-- form start -->
            <form id="dataForm" class="form-horizontal">
              <input type="hidden" id="id">
              <div class="box-body">

                <div class="form-group">
                  <label for="name" class="col-sm-2 control-label">素材名称</label>

                  <div class="col-sm-5">
                    <input type="text" name="name" maxlength="20" class="form-control" id="name">
                  </div>
                </div>
                <div class="form-group">
                  <label for="lyid" class="col-sm-2 control-label">联运子渠道ID：</label>
                  <div class="col-sm-2">
                    <input type="text" class="form-control" id="lyid" name="lyid" placeholder="联运平台子渠道id">
                  </div>
                </div>

                <div class="form-group">
                  <label for="gameid" class="col-sm-2 control-label">所属游戏</label>
                  <div class="col-sm-3">
                    <select id="gameid" class="form-control" name="gameid">
                    </select>
                  </div>
                </div>

                <div class="form-group">
                  <label  class="col-sm-2 control-label">上传素材图</label>
                  <div class="col-sm-6">
                    <span id="p1Div" class="picDiv">
                        <input type="file" name="mainImages" id="mainImage1"  style="display:none;"/>
                        <a href="javascript:void(0);" onclick="AddImg()">
                          <img id="mainPicture1" src="/dist/img/addimg1.jpg" />
                        </a>
                    </span>
                    <span id="p2Div" class="picDiv">
                      <input type="file" name="mainImages" id="mainImage2"  style="display:none;"/>
                      <a href="javascript:void(0);" onclick="AddImg2()">
                        <img id="mainPicture2" src="/dist/img/addimg1.jpg" />
                      </a>
                    </span>
                    <span id="p3Div" class="picDiv">
                      <input type="file" name="mainImages" id="mainImage3"  style="display:none;"/>
                      <a href="javascript:void(0);" onclick="AddImg3()">
                        <img id="mainPicture3" src="/dist/img/addimg1.jpg" />
                      </a>
                    </span>
                    <span id="p4Div" class="picDiv">
                      <input type="file" name="mainImages" id="mainImage4"  style="display:none;"/>
                      <a href="javascript:void(0);" onclick="AddImg4()">
                        <img id="mainPicture4" src="/dist/img/addimg1.jpg" />
                      </a>
                    </span>
                    <span id="p5Div" class="picDiv">
                      <input type="file" name="mainImages" id="mainImage5"  style="display:none;"/>
                      <a href="javascript:void(0);" onclick="AddImg5()">
                        <img id="mainPicture5" src="/dist/img/addimg1.jpg" />
                      </a>
                  </span>
                  </div>

                  <!--<div class="col-sm-4" id="picDiv">
                    <input type="hidden" name="pic" id="pic"/>
                    <div class="imgBox" style="display:none;">
                      <input type="file"  name="imgFile" style="display:none;"/>
                      <a href="javascript:void(0);" onclick="AddImg()">
                        <img  src="/dist/img/addimg.jpg"  />
                      </a>
                      <span class="badge badge-danger" style="margin-bottom:15px;cursor: pointer;" onclick="delPic(this);">删除</span>
                    </div>
                    <input type="file" id="mainImage" name="imgFile" style="display:none;"/>
                    <a href="javascript:void(0);" onclick="AddImg()">
                      <img id="mainPicture" src=""  />
                    </a>
                    <button type="button" class="btn btn-xs btn-info" onclick="addPic(this);">继续上传图片</button>
                  </div>-->


                </div>

                <div class="form-group">
                  <label for="page_url" class="col-sm-2 control-label">落地页地址</label>

                  <div class="col-sm-5">
                    <input type="text" name="page_url" class="form-control" id="page_url">
                  </div>
                </div>

              </div>

              <!-- /.box-body -->
              <div class="box-footer">
                <button type="button" onclick="goBack();" class="btn btn-default">取消</button>
                <button type="button" onclick="submitForm()" class="btn btn-primary">提交</button>
              </div>
              <!-- /.box-footer -->
            </form>
          </div>
        </div>
      </div>
    </section>
  </div>
  <!--底部-->
  <footer id="footerDiv" class="main-footer">
    <!--#include  file="/pages/admin/footer.html"-->
  </footer>

  <!-- Control Sidebar -->
  <!-- /.control-sidebar -->
  <!-- Add the sidebar's background. This div must be placed
       immediately after the control sidebar -->
  <div class="control-sidebar-bg"></div>
</div>
<script src="/bower_components/jquery/dist/jquery.min.js"></script>
<script src="/bower_components/morris.js/morris.min.js"></script>
<script src="/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="/bower_components/datatables.net/js/jquery.dataTables.min.js"></script>
<script src="/bower_components/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>
<script src="/bower_components/jquery-slimscroll/jquery.slimscroll.min.js"></script>
<script src="/bower_components/fastclick/lib/fastclick.js"></script>
<script src="/dist/js/adminlte.min.js"></script>

<script src="/dist/js/layer/layer.js"></script>
<script src="/dist/js/laydate/laydate.js"></script>
<script src="/dist/js/bootstrapValidator.min.js"></script>

<script src="/dist/js/uploadPreview.min.js"></script>
<script src="/dist/js/ajaxfileupload.js"></script>

<script>
    var flag=true;
    var uid;
    var sizeMap={};
    var typeMap={};
    var imgNum = 0;

    $("#mainImage1").uploadPreview({
        Img : "mainPicture1",
        Width : 200,
        Height : 100
    });
    $("#mainImage2").uploadPreview({
        Img : "mainPicture2",
        Width : 200,
        Height : 100
    });
    $("#mainImage3").uploadPreview({
        Img : "mainPicture3",
        Width : 200,
        Height : 100
    });
    $("#mainImage4").uploadPreview({
        Img : "mainPicture4",
        Width : 200,
        Height : 100
    });
    $("#mainImage5").uploadPreview({
        Img : "mainPicture5",
        Width : 200,
        Height : 100
    });

    function AddImg() {
        // a标签绑定onclick事件
        $('#mainImage1').click();
    }
    function AddImg2() {
        $('#mainImage2').click();
    }
    function AddImg3() {
        $('#mainImage3').click();
    }
    function AddImg4() {
        $('#mainImage4').click();
    }
    function AddImg5() {
        $('#mainImage5').click();
    }

    $(function () {
        uid = store.get("uid");//渠道id



        //加载游戏列表
        initGameList();




    });

    function submitForm() {

        let formMap={};
        formMap["lyid"] = $("#lyid").val();
        formMap["name"] = $("#name").val();
        formMap["game_id"] = $("#gameid").val();
        formMap["game_name"] = $("#gameid").find("option:selected").text();
        formMap["uid"] = uid;
        formMap["page_url"] = $("#page_url").val();

        let url="/lmgame/sucai/saveSucai";

        let id =$("#id").val();
        if(id !== ''){
            formMap["id"] = id;
        }

        $.ajaxFileUpload({
            url: url, //用于文件上传的服务器端请求地址
            type: 'POST',
            fileElementId: ['mainImage1','mainImage2','mainImage3','mainImage4','mainImage5'], //文件上传域的ID
            dataType: 'json', //返回值类型 一般设置为json
            data: formMap,
            success: function(data) {
                // $("#infoConfirm").modal("hide");
                if(data.code === 0) {
                    layer.msg('保存成功', {
                        icon: 1,
                        time: 1000 //3秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        flag=true;
                        window.location.href = "/pages/webmaster/sucaiList.html";
                    });
                } else {
                    layer.msg(data.msg);
                }
            },
            error: function (xhr, ts, et) { //XMLHttpRequest, textStatus, errorThrown
                if (xhr.status === 400) {
                    window.location.href = "/pages/admin/login.html";
                }
                if (xhr.status === 403) {
                    window.location.href = "/403.html";
                }
            }
        });
    }

    function  goBack() {
        window.history.back(-1);
    }


    function initGameList() {
        let url="/lmgame/game/gameList";
        $.ajax({
            type : 'POST',
            url : url,
            data :{},
            dataType : "json",
            success: function(data) {
                let gameList=data.data;
                let str='<option  value="">请选择</option>';
                for(let i=0;i<gameList.length;i++){
                    let game = gameList[i];
                    str +='<option  value="'+game.id+'">'+game.name+'</option>';
                }
                $("#gameid").html(str);

                //加载素材信息
                initSucai();
            }
        });

    }
    function initSucai() {
        let sucaiId=store.get('sucaiId');
        if(sucaiId !=''){
            //加载初始化数据
            let url="/lmgame/sucai/getSucaiById";
            $.ajax({
                type : 'POST',
                url : url,
                data :{'id':sucaiId},
                dataType : "json",
                success: function(data) {
                    data=data.data;
                    $("#id").val(data.id);
                    $("#lyid").val(data.lyid);
                    $("#name").val(data.name);
                    $("#gameid").val(data.game_id);
                    $("#page_url").val(data.page_url);
                    //组装图片信息

                    let imgs=data.images;
                    if(imgs!=""&&imgs!=null){
                        $(".picDiv").hide();
                        imgs=imgs.split(",");
                        for(let i=0;i< imgs.length;i++){
                            let r=i+1;
                            $("#mainPicture"+r).attr("src", imgs[i]);
                            $("#p"+r+"Div").show();
                        }
                    }


                },
                error:function (data) {
                }
            });
        }

    }



   /* function addPic(obj){
        var el=$(".imgBox").eq(0).clone();
        el.show();
        el.find("input[name='imgFile']").show();
        $(obj).before(el);
        //uploadImageFn.uploadFileSettingNotValidFile();
    }

    function delPic(obj){
        $(obj).parents(".imgBox").remove();
    }*/

    Date.prototype.Format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //日
            "H+": this.getHours(), //小时
            "m+": this.getMinutes(), //分
            "s+": this.getSeconds(), //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    };


</script>
</body>
</html>
